﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace Simp.Model.CoreExpressions.Sequences
{
	/// <summary>Sequence function taking two parameters:  a first and last.</summary>
	/// <remarks>
	/// The two extrema of the interval are the first and the last.  They do not need to be ordered:
	/// the sequence will have increasing or decreasing value, depending on the order of the extrema.
	/// Interval is defined <c>inclusively</c> with the extrema, ie, the extrama are included in the extrema.
	/// The extrema needs to simplify into an <see cref="IntegerExpression"/> in order for the sequence to be accessed.
	/// </remarks>
	[ExpressionName(IntervalSequenceExpressionName)]
	[ParameterPolicy("first", "last")]
	[DebuggerDisplay("{ExpressionHelper.Serialize(this)}")]
	public class IntervalSequenceExpression : ISequenceExpression, IExpressionSerializer
	{
		#region Inner Types
		private class IntervalSequenceAccessor : ISequenceAccessor
		{
			private readonly int _first;
			private readonly int _last;

			public IntervalSequenceAccessor(int first, int last)
			{
				_first = first;
				_last = last;
			}

			#region ISequenceAccessor Members
			int ISequenceAccessor.Count
			{
				get { return Math.Abs(_last - _first) + 1; }
			}

			IExpression ISequenceAccessor.this[int index]
			{
				get
				{
					if (index < 0 || index >= ((ISequenceAccessor)this).Count)
					{
						throw new IndexOutOfRangeException();
					}

					return (_last > _first)
						? new IntegerExpression(_first + index)
						: new IntegerExpression(_first - index);
				}
			}
			#endregion

			#region IEnumerable<IExpression> Members
			IEnumerator<IExpression> IEnumerable<IExpression>.GetEnumerator()
			{
				if (_last >= _first)
				{
					for (int i = _first; i <= _last; ++i)
					{
						yield return new IntegerExpression(i);
					}
				}
				else
				{
					for (int i = _first; i >= _last; --i)
					{
						yield return new IntegerExpression(i);
					}
				}
			}
			#endregion

			#region IEnumerable Members
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<IExpression>)this).GetEnumerator();
			}
			#endregion
		}
		#endregion

		/// <summary>Name of the expression.</summary>
		public const string IntervalSequenceExpressionName = "IntervalSequence";

		#region ISequenceExpression Members
		ISequenceAccessor ISequenceExpression.GetAccessor(params IExpression[] parameters)
		{
			if (parameters == null)
			{
				throw new ArgumentNullException("parameters");
			}
			if (parameters.Length != 2)
			{
				throw new ArgumentException("Wrong # of parameters", "parameters");
			}

			IntegerExpression integerFirst = parameters[0] as IntegerExpression;
			IntegerExpression integerLast = parameters[1] as IntegerExpression;

			if (integerFirst != null && integerLast != null)
			{
				return new IntervalSequenceAccessor(integerFirst.Value, integerLast.Value);
			}
			else
			{
				return null;
			}
		}
		#endregion

		#region IExpressionSerializer Members
		void IExpressionSerializer.Serialize(AppendOnlyStringBuilder builder, params IExpression[] parameters)
		{
			if (builder == null)
			{
				throw new ArgumentNullException("builder");
			}
			if (parameters == null)
			{
				throw new ArgumentNullException("parameters");
			}
			if (parameters.Length == 2)
			{
				IExpression first = parameters[0];
				IExpression last = parameters[1];

				builder.Append("{");
				ExpressionHelper.Serialize(builder, first);
				builder.Append(" .. ");
				ExpressionHelper.Serialize(builder, last);
				builder.Append("}");
			}
		}
		#endregion
	}
}